//
//  HomeVC.swift
//  RPay
//
//  Created by Charlie Liao on 2017/6/27.
//  Copyright © 2017年 Charlie. All rights reserved.
//

import UIKit

class HomeVC: BaseVC, CTAPIManagerCallBackDelegate {
    
    // MARK: - Init
    convenience init() {
        self.init(nibName: "HomeView", bundle: nil)
    }
    
    // MARK: - Life Cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        //将gradientLayer作为子layer添加到主layer上
        headerView.layer.insertSublayer(gradientLayer, at: 0)
        // Do any additional setup after loading the view.
        NotificationCenter.default.addObserver(self, selector:#selector(getUserInfor) , name:NSNotification.Name(rawValue: "updateuserinfor"), object: nil)

        
        
    }
     func getUserInfor() {
        let user = ShareUser()
        
        user.setUserId((UserShare.shared.loginUser?.userId)!, userName: UserShare.shared.loginUser?.userName, token: UserShare.shared.loginUser?.token, rhAccount: (UserShare.shared.loginUser?.rhAccount)!)
        
//        ShareUser.shareUser().userId = (UserShare.shared.loginUser?.userId)!
//        ShareUser.shareUser().userName = UserShare.shared.loginUser?.userName
//        ShareUser.shareUser().rhAccount = UserShare.shared.loginUser?.rhAccount
        
        
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        gradientLayer.frame = headerView.bounds
    }
    
    override func viewWillAppear(_ animated: Bool) {
        navigationController!.setNavigationBarHidden(true, animated: animated)
        super.viewWillAppear(animated)
        balanceLabel.text = String(format: "%.2f", UserShare.shared.loginUser!.rhAccount)
        if _apiManager == nil {
            apiManager.loadData()
        }
    }
    
    // MARK: - Api Callback
    func managerCallAPIDidSuccess(_ manager: CTAPIBaseManager!)
    {
        let balance = manager.fetchData(with: BalanceReformer())  as! Double
        UserShare.shared.loginUser!.rhAccount = balance
        balanceLabel.text = String(format: "%.2f", balance)
        _apiManager = nil
    }
    
    func managerCallAPIDidFailed(_ manager: CTAPIBaseManager!)
    {
        _apiManager = nil
    }
    
    // MARK: - Event repsond
    /// 点击扫一扫
    @IBAction func tapQRcodeBtn() {
        let vc = QRCodeVC()
        vc.hidesBottomBarWhenPushed = true
        navigationController!.pushViewController(vc, animated: true)
    }
    
    /// 点击付款
    @IBAction func tapQRPayBtn() {
        let vc = QRPayVC()
        vc.hidesBottomBarWhenPushed = true
        navigationController!.pushViewController(vc, animated: true)
    }
    
    /// 点击收款
    @IBAction func tapQRReceiptBtn() {
        let vc = QRReceiptVC()
        vc.hidesBottomBarWhenPushed = true
        navigationController!.pushViewController(vc, animated: true)
    }
    
    // MARK: - Get
    
    @IBOutlet weak var headerView: UIView!
    /// 余额Label
    @IBOutlet weak var balanceLabel: UILabel!
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
    lazy var gradientLayer: CAGradientLayer = {
        let gradientLayer = CAGradientLayer()
        //设置渐变的主颜色
        gradientLayer.colors = [UIColor(hex: 0xe4b77a).cgColor, UIColor.appTheme.cgColor]
        gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
        gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
        return gradientLayer
    }()
    
    private var _apiManager: UserInfoApiManager?
    private var apiManager: UserInfoApiManager {
        if _apiManager == nil {
            _apiManager = UserInfoApiManager()
            _apiManager!.delegate = self
        }
        return _apiManager!
    }
}
